<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>$emit的使用</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id="root">
    <div :style="{ fontSize: postFontSize + 'em' }">
      <blog-post v-for="post in posts" v-bind:key="post.id" v-bind:post="post"
        v-on:enlarge-text="postFontSize += $event"></blog-post>

      <blog-post v-for="post in posts" v-bind:key="post.id" v-bind:post="post"
        v-on:enlarge-text="change"></blog-post>
    </div>
  </div>

</body>

<!--  
$emit 可以接收父组件绑定的事件
父组件只需要使用v-on绑定事件名即可
$emit可以传递参数
$event可以接收参数
如果父组件绑定了函数那么传参则会变成函数的入参返回
 -->


<script type="text/javascript">
  Vue.config.productionTip = false;

  Vue.component("blog-post", {
    props: ['post'],
    template: `<div><button @click="$emit('enlarge-text',0.1)">变大</button><h1>{{post.title}}</h1></div>`,
  })


  new Vue({
    el: '#root',
    data: {
      posts: [
        { id: 1, title: 'My journey with Vue' },
        { id: 2, title: 'Blogging with Vue' },
        { id: 3, title: 'Why Vue is so fun' }   
      ],
      postFontSize: 1
    },
    methods: {
      change:function(size){
        this.postFontSize += size
      }
    }
  })
</script>

</html>